package com.chezai.client.server.util;

import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.NoSuchElementException;

/**
 * Describe
 * Created by LZF on 2016/9/6.
 */
public class PassUtil {

//    public PrivateKey getDevCAInfo(String cert, String certPassword) throws Exception {
//        char[] password = certPassword.toCharArray();
//        byte[] cabuf = Base64.decode(cert.getBytes(),0);
//        KeyStore keyStore = KeyStore.getInstance("PKCS12");
//        keyStore.load(new ByteArrayInputStream(cabuf), password);
//        Enumeration<String> aliases = keyStore.aliases();
//        if (!aliases.hasMoreElements()) {
//            throw new RuntimeException("no alias found");
//        }
//        String alias = aliases.nextElement();
//        PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, password);// 私钥
//        return privatekey;
//    }
//
//    public static X509Certificate getPublicCert(String key, String pass) throws Exception {
//        char[] password = pass.toCharArray();
//        byte[] cabuf = Base64.decode(key.getBytes(),0);
//        KeyStore keyStore = KeyStore.getInstance("PKCS12");
//        keyStore.load(new ByteArrayInputStream(cabuf), password);
//        Enumeration aliases = keyStore.aliases();
//        if(!aliases.hasMoreElements()) {
//            throw new RuntimeException("no alias found");
//        } else {
//            String alias = (String)aliases.nextElement();
//            X509Certificate cert = (X509Certificate)keyStore.getCertificate(alias);
//            return cert;
//        }
//    }
public PrivateKey getDevCAInfo(String cert, String certPassword) throws Exception {
    char[] password = certPassword.toCharArray();
    byte[] cabuf = Base64.decode(cert.getBytes(), 0);
    KeyStore keyStore = KeyStore.getInstance("PKCS12");


    ByteArrayInputStream in = new ByteArrayInputStream(cabuf);
    ByteArrayOutputStream out = new ByteArrayOutputStream();


    keyStore.load(in, password);
    keyStore.store(out, password);

    Enumeration<String> aliases = keyStore.aliases();
    if (!aliases.hasMoreElements()) {
        throw new RuntimeException("no alias found");
    }
    String alias = null;
    while (true) {
        try {
            alias = aliases.nextElement();
            System.out.println(alias);
        } catch (NoSuchElementException e) {
            System.out.println("读取完毕");
            break;
        }
    }
    PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, password);
    return privatekey;
}

    public PrivateKey getDevCAInfo2(String cert, String certPassword) throws Exception {
        char[] password = certPassword.toCharArray();
        byte[] cabuf = Base64.decode(cert.getBytes(), 0);
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(new ByteArrayInputStream(cabuf), password);
        Enumeration<String> aliases = keyStore.aliases();
        if (!aliases.hasMoreElements()) {
            throw new RuntimeException("no alias found");
        }
        String alias = aliases.nextElement();
        PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, password);
        return privatekey;
    }


    public static X509Certificate getPublicCert(String key, String pass) throws Exception {
        char[] password = pass.toCharArray();
        byte[] cabuf = Base64.decode(key.getBytes(), 0);
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(new ByteArrayInputStream(cabuf), password);
        Enumeration aliases = keyStore.aliases();
        if (!aliases.hasMoreElements()) {
            throw new RuntimeException("no alias found");
        } else {
            String alias = (String) aliases.nextElement();
            X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
            return cert;
        }
    }
}
